package com.sgb.kjwl.viewmodel.freightTransport;


import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.NeedsEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.HomeRepertory;
import com.sgb.kjwl.view.ui.fragment.freightTransport.LogisticsSearchListFragment3;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class LogisticsListModel3 extends BaseViewModel {
    private LogisticsSearchListFragment3 fragment;
    private HomeRepertory repertory;
    private Map map;
    private int pageNum;
    private int pageSize = 5;
    private int total;
    private String areaCode;

    public LogisticsListModel3(LogisticsSearchListFragment3 fragment, HomeRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;
    }

    //卖家推荐需求
    public void getNeedsList(String cityNo, String areaNo, int type) {
        if (map == null) {
            map = new HashMap();
        }
        pageNum = 1;
        if (type == 1) {
            areaCode = repertory.getAreaCode();
        } else {
            areaCode = "";
        }
        map.put("pageNum", pageNum);
        map.put("pageSize", pageSize);
        map.put("areaCode", areaCode);
        map.put("cityNo", cityNo);
        map.put("districtNo", areaNo);
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
            APIManager.getInstance().getCommonAPI().getHomeNeedsList(map).enqueue(new Callback<BaseEntity<ListResponse<List<NeedsEntity>>>>() {
                @Override
                public void onResponse(Call<BaseEntity<ListResponse<List<NeedsEntity>>>> call, Response<BaseEntity<ListResponse<List<NeedsEntity>>>> response) {
                    DialogHelper.dismissProgressDialog();
                    if (response.body() == null)
                        return;
                    if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                        if (response.body().getData() == null)
                            return;
                        if (response.body().getData().getList() == null)
                            return;
                        total = response.body().getData().getTotal();
                        if (total > 0) {
                            fragment.refresh(response.body().getData().getList(), response.body().getData().isLastPage());
                        } else {
                            fragment.empty();
                        }
                    } else {
                        MToast.showToast(fragment.getActivity(), response.message());
                    }
                }

                @Override
                public void onFailure(Call<BaseEntity<ListResponse<List<NeedsEntity>>>> call, Throwable t) {
                    DialogHelper.dismissProgressDialog();
                    MToast.showToast(fragment.getActivity(), t.getMessage());
                }
            });
    }

    //更多
    public void getMoreList(String cityNo, String areaNo, int type) {
        if (map == null) {
            map = new HashMap();
        }
        if (type == 1) {
            areaCode = repertory.getAreaCode();
        } else {
            areaCode = "";
        }
        pageNum = pageNum++;
        map.put("pageNum", pageNum);
        map.put("pageSize", pageSize);
        map.put("areaCode", areaCode);
        map.put("cityNo", cityNo);
        map.put("districtNo", areaNo);
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
            APIManager.getInstance().getCommonAPI().getHomeNeedsList(map).enqueue(new Callback<BaseEntity<ListResponse<List<NeedsEntity>>>>() {
                @Override
                public void onResponse(Call<BaseEntity<ListResponse<List<NeedsEntity>>>> call, Response<BaseEntity<ListResponse<List<NeedsEntity>>>> response) {
                    DialogHelper.dismissProgressDialog();
                    if (response.body() == null)
                        return;
                    if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                        if (response.body().getData() == null)
                            return;
                        if (response.body().getData().getList() == null)
                            return;
                        fragment.loadMore(response.body().getData().getList(), response.body().getData().isLastPage());
                    } else {
                        MToast.showToast(fragment.getActivity(), response.message());
                    }
                }

                @Override
                public void onFailure(Call<BaseEntity<ListResponse<List<NeedsEntity>>>> call, Throwable t) {
                    DialogHelper.dismissProgressDialog();
                    MToast.showToast(fragment.getActivity(), t.getMessage());
                }
            });
    }



    public void saveProvinceModel(String name, String code) {
        //实时保存ProvinceCode，为module使用
        SharedPreferenceUtil.getInstance().saveString(SharedPreferenceUtil.AppProvinceCode,code);
        repertory.setAreaCode(code);
        repertory.setAreaName(name);
    }
}
